package com.oot2d.entity;

import org.newdawn.slick.Animation;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.geom.Vector2f;

import java.util.List;

public class Entity {
    protected SpriteSheet sheet;
    protected Animation[] anims;
    protected Animation currAnim;
    protected Direction dir;
    protected int id;
    protected Vector2f vel;
    protected float x, y;
    protected int z;
    protected int width, height;
    public AABB aabb;

    public Entity(String s, int w, int h)
    {
        try {
            sheet = new SpriteSheet("res/sprites/" + s + ".png", w, h);
        } catch (SlickException e) {
            System.err.println(e.getMessage());
        }

        dir = Direction.Down;
        vel = new Vector2f(0.0f, 0.0f);
        x = 0.0f;
        y = 0.0f;
        z = 0;
        width = w;
        height = h;
    }

    public Entity(String s, int w, int h, int a)
    {
        try {
            sheet = new SpriteSheet("res/sprites/" + s + ".png", w, h);
        } catch (SlickException e) {
            System.err.println(e.getMessage());
        }

        anims = new Animation[a];
        dir = Direction.Down;
        vel = new Vector2f(0.0f, 0.0f);
        x = 0.0f;
        y = 0.0f;
        z = 0;
        width = w;
        height = h;
    }

    public Entity(Entity e)
    {
        sheet = e.sheet;
        anims = e.anims;
        dir = e.dir;
        id = e.id;
        vel = new Vector2f(e.vel);
        x = 0.0f;
        y = 0.0f;
        z = 0;
        width = e.width;
        height = e.height;

        if (e.aabb != null)
            aabb = new AABB(e.aabb, this);
    }

    public Entity(String s, int w, int h, float bw, float bh, float xoff, float yoff)
    {
        try {
            sheet = new SpriteSheet("res/sprites/" + s + ".png", w, h);
        } catch (SlickException e) {
            System.err.println(e.getMessage());
        }

        dir = Direction.Down;
        vel = new Vector2f(0.0f, 0.0f);
        x = 0.0f;
        y = 0.0f;
        z = 0;
        width = w;
        height = h;
        aabb = new AABB(bw, bh, xoff, yoff, this);
    }

    public Entity(String s, int w, int h, float bw, float bh, float xoff, float yoff, int a)
    {
        try {
            sheet = new SpriteSheet("res/sprites/" + s + ".png", w, h);
        } catch (SlickException e) {
            System.err.println(e.getMessage());
        }

        anims = new Animation[a];
        dir = Direction.Down;
        vel = new Vector2f(0.0f, 0.0f);
        x = 0.0f;
        y = 0.0f;
        z = 0;
        width = w;
        height = h;
        aabb = new AABB(bw, bh, xoff, yoff, this);
    }

    public float getX()
    {
        return x;
    }

    public float getY()
    {
        return y;
    }

    public int getZ()
    {
        return z;
    }

    public float getCenterX()
    {
        return x + (width / 2.0f);
    }

    public float getCenterY()
    {
        return y + (height / 2.0f);
    }

    public int getWidth()
    {
        return width;
    }

    public int getHeight()
    {
        return height;
    }

    public void setZ(int sz)
    {
        z = sz;
    }

    public void setPos(float px, float py)
    {
        x = px;
        y = py;
    }

    public void setPos(Vector2f v)
    {
        x = v.x;
        y = v.y;
    }

    public void setDir(Direction d)
    {
        dir = d;
    }

    public void setID(int i)
    {
        id = i;
    }

    public void addAnims(List<List<Object>> l)
    {
        int i = 0;
        for (List<Object> n : l) {
            Object[] o = n.toArray();
            anims[i] = new Animation(sheet, (Integer)o[0], (Integer)o[1], (Integer)o[2],
                    (Integer)o[3], true, (Integer)o[4], true);
            i++;
        }
    }

    public void update(int d)
    {

    }

    public void draw(Graphics g)
    {
        sheet.startUse();
        sheet.getSubImage(dir.val(), 0).drawEmbedded(x, y, width, height);
        sheet.endUse();
        g.drawString(((Integer)id).toString(), x, y - 5);
    }
}
